﻿namespace Pub.Class
{
    using System;
    using System.Runtime.CompilerServices;
    using System.Text.RegularExpressions;

    public static class DecimalExtensions
    {
        public static decimal IfEqual(this decimal obj, decimal value, decimal defaultValue)
        {
            if (!(obj == value))
            {
                return obj;
            }
            return defaultValue;
        }

        public static decimal IfLessThan(this decimal obj, decimal value, decimal defaultValue)
        {
            if (obj >= value)
            {
                return obj;
            }
            return defaultValue;
        }

        public static decimal IfLessThanOrEqual(this decimal obj, decimal value, decimal defaultValue)
        {
            if (obj > value)
            {
                return obj;
            }
            return defaultValue;
        }

        public static decimal IfMoreThan(this decimal obj, decimal value, decimal defaultValue)
        {
            if (obj <= value)
            {
                return obj;
            }
            return defaultValue;
        }

        public static decimal IfMoreThanOrEqual(this decimal obj, decimal value, decimal defaultValue)
        {
            if (obj < value)
            {
                return obj;
            }
            return defaultValue;
        }

        public static decimal IfNotEqual(this decimal obj, decimal value, decimal defaultValue)
        {
            if (!(obj != value))
            {
                return obj;
            }
            return defaultValue;
        }

        public static decimal PercentageOf(this decimal number, decimal percent)
        {
            return ((number * percent) / 100M);
        }

        public static decimal PercentageOf(this decimal number, int percent)
        {
            return ((number * percent) / 100M);
        }

        public static decimal PercentageOf(this decimal number, long percent)
        {
            return ((number * percent) / 100M);
        }

        public static decimal PercentOf(this decimal position, decimal total)
        {
            decimal num = 0M;
            if ((position > 0M) && (total > 0M))
            {
                num = (position / total) * 100M;
            }
            return num;
        }

        public static decimal PercentOf(this decimal position, int total)
        {
            decimal num = 0M;
            if ((position > 0M) && (total > 0))
            {
                num = (position / total) * 100M;
            }
            return num;
        }

        public static decimal PercentOf(this decimal position, long total)
        {
            decimal num = 0M;
            if ((position > 0M) && (total > 0L))
            {
                num = (position / total) * 100M;
            }
            return num;
        }

        public static decimal Round(this decimal val, int decimalPoints)
        {
            return Math.Round(val, decimalPoints);
        }

        public static decimal Round2(this decimal val)
        {
            return Math.Round(val, 2);
        }

        public static string ToCurrency(this decimal value)
        {
            return value.ToString("N");
        }

        public static string ToRMB(this decimal input)
        {
            return Regex.Replace(Regex.Replace(input.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A"), @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}"), ".", (MatchEvaluator) (m => "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟萬億兆京垓秭穰"[m.Value[0] - '-'].ToString()));
        }

        public static string ToRMB2(this decimal num)
        {
            string str = "零壹贰叁肆伍陆柒捌玖";
            string str2 = "万仟佰拾亿仟佰拾万仟佰拾元角分";
            string str3 = "";
            string str4 = "";
            string str5 = "";
            string str6 = "";
            string str7 = "";
            int num2 = 0;
            num = Math.Round(Math.Abs(num), 2);
            str4 = ((long) (num * 100M)).ToString();
            int length = str4.Length;
            if (length > 15)
            {
                return "溢出";
            }
            str2 = str2.Substring(15 - length);
            for (int i = 0; i < length; i++)
            {
                str3 = str4.Substring(i, 1);
                int startIndex = Convert.ToInt32(str3);
                if (((i != (length - 3)) && (i != (length - 7))) && ((i != (length - 11)) && (i != (length - 15))))
                {
                    if (str3 == "0")
                    {
                        str6 = "";
                        str7 = "";
                        num2++;
                    }
                    else if ((str3 != "0") && (num2 != 0))
                    {
                        str6 = "零" + str.Substring(startIndex, 1);
                        str7 = str2.Substring(i, 1);
                        num2 = 0;
                    }
                    else
                    {
                        str6 = str.Substring(startIndex, 1);
                        str7 = str2.Substring(i, 1);
                        num2 = 0;
                    }
                }
                else if ((str3 != "0") && (num2 != 0))
                {
                    str6 = "零" + str.Substring(startIndex, 1);
                    str7 = str2.Substring(i, 1);
                    num2 = 0;
                }
                else if ((str3 != "0") && (num2 == 0))
                {
                    str6 = str.Substring(startIndex, 1);
                    str7 = str2.Substring(i, 1);
                    num2 = 0;
                }
                else if ((str3 == "0") && (num2 >= 3))
                {
                    str6 = "";
                    str7 = "";
                    num2++;
                }
                else if (length >= 11)
                {
                    str6 = "";
                    num2++;
                }
                else
                {
                    str6 = "";
                    str7 = str2.Substring(i, 1);
                    num2++;
                }
                if ((i == (length - 11)) || (i == (length - 3)))
                {
                    str7 = str2.Substring(i, 1);
                }
                str5 = str5 + str6 + str7;
                if ((i == (length - 1)) && (str3 == "0"))
                {
                    str5 = str5 + '整';
                }
            }
            if (num == 0M)
            {
                str5 = "零元整";
            }
            return str5;
        }
    }
}

